'pure function'

pure function

Information

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Modelica functions are normally pure which makes it easy for humans to reason about the code since they behave as mathematical functions, and possible for compilers to optimize.

  • Pure Modelica functions always give the same output values or errors for the same input values and only the output values influence the simulation result, i.e. is seen as equivalent to a mathematical map from input values to output values. Some input values may map to errors. Pure functions are thus allowed to fail by calling assert, or ModelicaError in C-code, or dividing by zero. Such errors will only be reported when and if the function is called. Pure Modelica functions are not assumed to be thread-safe.
  • A Modelica function which does not have the pure function properties is impure.

The declaration of functions follows these rules:

  • Functions defined in Modelica (non-external) are normally assumed to be pure (the exception is the deprecated case below), if they are impure they shall be marked with the impure keyword. They can be explicitly marked as pure.
  • External functions must be explicitly declared with pure or impure.
  • A deprecated semantics is that external functions (and functions defined in Modelica directly or indirectly calling them) without pure or impure keyword are assumed to be impure – but without any restriction on calling them. Except for the function Modelica.Utilities.Streams.print diagnostics must be given if called in a simulation model.